<?php
namespace zhtfrontend\controllers;

use common\components\extenders\AjaxObject;
use common\components\extenders\MenuManage;
use yii\helpers\Json;
use zhtfrontend\components\bases\BaseController;
use zhtfrontend\models\datas\GoodsClassTime;
use yii\web\BadRequestHttpException;
use zhtfrontend\models\datas\Goods;
use common\models\datas\Cate;
use yii\data\Pagination;
use common\components\helpers\Fun;
use common\models\datas\CateAttr;
use common\components\helpers\Tree;
use zhtfrontend\models\datas\GoodsTeacher;
use yii\web\NotFoundHttpException;
use yii\web\Response;

class CharacteristicController extends BaseController{

    public function actionIndex()
    {
        $pageSize = 12;
        $param=\Yii::$app->request->get();
        $goods=Goods::find()->where(['type_id'=>1,'is_delete'=>0])->andWhere(['>','menu_id',0]);
        if(isset(\Yii::$app->user->identity->role) && \Yii::$app->user->identity->role == 'category'){
            $goods->andWhere(['category_id'=>\Yii::$app->user->identity->type_id]);
        }
        if (!empty($param['goods_name'])) {
            $goods->andWhere(['like', 'name', $param['goods_name']]);
        }
        $pages = new Pagination(['totalCount' => $goods->count(), 'pageSize' => $pageSize,'params'=>$param]);
        isset($param['page']) ? $pages->page = $param['page'] - 1 : 0;
        $model = $goods->offset($pages->offset)->limit($pages->limit)->all();
        $data = Fun::asArray($model, ['cate_name','class_time','room_name','teacherStr']);

        return $this->render('index',['data'=>$data,'pages'=>$pages]);
    }

    public function actionCreate(){
        $this->layout = '@app/views/layouts/iframe.php';
        if (\Yii::$app->request->isAjax) {
            if (\Yii::$app->request->post('param') != null) $param = Json::decode(\Yii::$app->request->post('param'));
            \Yii::$app->response->format = Response::FORMAT_JSON;
            $pageSize = isset($param['pageSize']) ? $param['pageSize'] : 12;
            //$param=\Yii::$app->request->get();
            $model=Goods::find()->where(['type_id'=>1,'is_delete'=>0,'menu_id'=>0])->andWhere(['>','store',0]);
            if(isset(\Yii::$app->user->identity->role) && \Yii::$app->user->identity->role == 'category'){
                $model->andWhere(['category_id'=>\Yii::$app->user->identity->type_id]);
            }
            if (!empty($param['searchData'])) {
                $search_data = array_filter($param['searchData']);
                if (!empty($search_data['goods_name'])) {
                    $model->andWhere(['like', 'name', $search_data['goods_name']]);
                }
                if (!empty($search_data['teacher_name'])) {
                    $ids = GoodsTeacher::find()->select('goods_id')->where(['like', 'teacher_name', $search_data['teacher_name']])->column();
                    $model->andWhere(['id'=>$ids]);
                }
                if (isset($search_data['is_shelve']) && $search_data['is_shelve'] != null) {
                    $model->andWhere(['is_shelve'=> $search_data['is_shelve']]);
                }
            }
            $pages = new Pagination(['totalCount' => $model->count(), 'pageSize' => $pageSize]);
            isset($param['page']) ? $pages->page = $param['page'] - 1 : 0;
            $model = $model->offset($pages->offset)->limit($pages->limit)->all();
            $data = Fun::asArray($model,['cate_name','class_time','room_name','teacherStr','shelve_status']);
            foreach ($data as $key=>$val){
                $data[$key]['set'] =(new MenuManage(MenuManage::TOP_MENU))->detailsMenu('characteristic','set',false,['rel'=>'set','class' => 'btn yx-radius2 yx-d-card yx-bg7ecef4','data'=>['id'=>$val['id']]]);
            }
            $ajax_object = new AjaxObject();
            $ajax_object->data = $data;
            $ajax_object->pages = $pages;
            return $ajax_object;
        }

        return $this->render('create');
    }

    public function actionRemove(){
        \Yii::$app->response->format = Response::FORMAT_JSON;
        $id = \Yii::$app->request->get('id');
        if ($id == null) {
            return ['status'=>0,'content'=>'系统错误！请重试'];
        }
        $model = Goods::findOne($id);
        if ($model == null) {
            return ['status'=>0,'content'=>'系统错误！请重试'];
        }
        $model->menu_id=0;
        if ($model->save(false)) {
            return ['status'=>1,'content'=>'移除成功！'];
        }
    }

    public function actionSet(){
        \Yii::$app->response->format = Response::FORMAT_JSON;
        $id = \Yii::$app->request->get('id');
        if($id == null){
            return 0;
        }
        $model = Goods::findOne($id);
        $model->menu_id = 3;
        if($model->save(false)){
            return 1;
        }else{
            return 0;
        }
    }
}